#include "srexportwidget.h"

#include <QLabel>
#include <QTextStream>
#include <QPushButton>
#include <QFileDialog>
#include <QVBoxLayout>
#include <QHBoxLayout>

SRExportWidget::SRExportWidget(const QString &_exkPubKey,
                               const int& _mode,
                               QWidget *parent)
    : QWidget(parent),ekPubKey_(_exkPubKey)
{

    setWindowTitle(tr("用户登录"));
    QWidget* mainWidget = new QWidget();

//    QWidget* wdgt_logo = new QWidget();
//    QLabel* lab_logo = new QLabel();
//    lab_logo->setAlignment(Qt::AlignCenter);
//    lab_logo->setPixmap(QPixmap(":/register/right"));
//    QLabel* lab_logo_title = new QLabel("导出公钥");
//    lab_logo_title->setAlignment(Qt::AlignCenter);
//    lab_logo_title->setObjectName(QString("LogoTitle"));


    QPushButton* btn_export = new QPushButton(tr("导出"));
    QPushButton* btn_done = new QPushButton(tr("完成"));
    QLabel* lab_schedule = new QLabel();
    if (_mode == 1)
        lab_schedule->setPixmap(QPixmap(":/register/nvgn_4"));
    else
        lab_schedule->setPixmap(QPixmap(":/register/nvgn4"));

    QVBoxLayout* layout = new QVBoxLayout();
    QHBoxLayout* layout_btn = new QHBoxLayout();
//    QGridLayout* layout_logo = new QGridLayout();

    // link
    connect(btn_done, &QPushButton::clicked,
            this, &SRExportWidget::sigFinished);
    connect(btn_export, &QPushButton::clicked,
            this, [=]{
        QString file_name = QFileDialog::getSaveFileName(this,tr("选择公钥导出路径"),".",
                                                         tr("Public Seeds(*.seed)"));
        QFile file(file_name);
        if (!file.open(QIODevice::Truncate
                  | QIODevice::WriteOnly)) {
            qWarning("write excel record failed,"\
                   "because sign file write failed.");
            return;
        }
        QTextStream in(&file);
        in << this->ekPubKey_;
        file.close();

    });


    // 规格
//    wdgt_logo->setFixedSize(200,130);
    btn_export->setFixedSize(100,40);
    btn_done->setFixedSize(100,40);
    mainWidget->setFixedSize(600,330);


    // 布局
    layout_btn->addWidget(btn_export);
    layout_btn->addSpacing(20);
    layout_btn->addWidget(btn_done);
//    layout_logo->addWidget(lab_logo,0,1,1,1);
//    layout_logo->addWidget(lab_logo_title,1,1,1,1);
//    wdgt_logo->setLayout(layout_logo);
//    layout->addWidget(wdgt_logo,0,Qt::AlignHCenter);
    layout->addWidget(lab_schedule,0,Qt::AlignTop | Qt::AlignHCenter);
    layout->addLayout(layout_btn);

    mainWidget->setLayout(layout);

    // @marker: 因为登录界面比较简单，没有外窗口
    //          所以这边模拟了一个外窗口
    QGridLayout* temp_layout = new QGridLayout();
    temp_layout->addWidget(mainWidget);
    temp_layout->setMargin(0);
    setLayout(temp_layout);
}
